package com.poizon.utils;

import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.poizon.config.SystemConfig;

/**
 * @ClassName CheckUtils
 * @Author NoCheng
 * @Date 2022/1/2 11:11
 * @Description:TODO 参数检查工具
 */

public class CheckUtils {
    //敏感词过滤器
    private static final SensitiveWordBs bs;
    static {
        bs = SensitiveWordBs.newInstance();
    }

    /**
     * 字符串非空校验*/
    public static boolean isNotEmpty(String... str) {
        for (String s : str) {
            if (s == null || s.trim().length() == 0) {
                return false;
            }
        }
        return true;
    }
    /**
     * 手机号格式校验*/
    public static boolean isPhone(String phone){
        if (isNotEmpty(phone)){
            return phone.matches(SystemConfig.REG_PHONE);
        }
        return false;
    }
    /**
     * 货号格式校验*/
    public static boolean isItemCode(String itemCode){
        if (isNotEmpty(itemCode)){
            return itemCode.matches(SystemConfig.REG_ITEM_CODE);
        }
        return false;
    }

    /**
     * 验证码格式校验*/
    public static boolean isCode(String code){
        if (isNotEmpty(code)){
            return code.matches(SystemConfig.REG_CODE);
        }
        return false;
    }

    /**
     * 动态的标题校验*/
    public static boolean checkTitle(String title){
        if (isNotEmpty(title)){
            //长度校验 + 敏感词校验
            return title.length() <= 20 && !bs.contains(title);
        }
        return true;
    }

    /**
     * 动态的内容校验*/
    public static boolean checkMomentContent(String momentContent){
        if (isNotEmpty(momentContent)){
            //长度校验 + 敏感词校验
            return momentContent.length() <= 1024 && !bs.contains(momentContent);
        }
        return true;
    }

    /**
     * 评论内容校验*/
    public static boolean checkComment(String comment){
        if (isNotEmpty(comment)){
            return comment.length() <= 128 && !bs.contains(comment);
        }
        return false;
    }
}
